# -*- coding:utf-8 -*-
# __auther__='jiangkai'

from selenium.webdriver.common.by import By
class ClickTimeControl:
    def __init__(self,driver,strtime):
        self.driver = driver
        self.strtime = strtime

    def clicktime(self,yearXPath=None,monthXPath=None,dayXPath=None):
        year = self.strtime[0:3]
        month = self.strtime[5:6]
        day = self.strtime[8:9]

        # self.driver.find_element_by_id("startContract").click()

        year_element = self.driver.find_elements(By.XPATH,yearXPath)
        print(year_element)

        for year_nr in year_element:
            if year_nr.text == year and year_nr.get_attribute("class") == "year" :
                year_nr.click()

        self.driver.find_element_by_xpath(monthXPath+"/span["+str(month)+"]").click()

        day_element_tr = self.driver.find_elements(By.XPATH,dayXPath)
        for day_nr_tr in day_element_tr :
            day_eles_td = day_nr_tr.driver.find_elements(By.XPATH,"td")
            for day_nr_tr in day_eles_td:
                if day_nr_tr.get_attribute("class") == "day" and day_nr_tr.text == day:
                    day_nr_tr.click()
                    break

    def click_year(self,yearXPath=None):
        year = self.strtime[0:4]
        year_element = self.driver.find_elements(By.XPATH, yearXPath)

        for year_nr in range(0,len(year_element)):
            if year_element[year_nr].text == str(int(year)):
                self.driver.find_element_by_xpath(yearXPath+"["+str(int(year_nr)+1)+"]").click()
                return

    def click_month(self,monthXPath=None):
        month = self.strtime[5:7]
        print(month)
        self.driver.find_element_by_xpath(monthXPath + "[" + str(month) + "]").click()

    def click_day(self,dayXPath=None):
        day = self.strtime[8:10]
        day_element_tr = self.driver.find_elements(By.XPATH, dayXPath)

        for day_nr_tr in range(0,len(day_element_tr)):
            day_eles_td = self.driver.find_elements(By.XPATH, dayXPath + "[" + str(day_nr_tr) + "]/td")
            for day_nr_td in range(0,len(day_eles_td)) :
                if day_eles_td[day_nr_td].text == str(int(day)) and day_eles_td[day_nr_td].get_attribute("class") == "day":
                    self.driver.find_element_by_xpath(dayXPath + "["+ str(day_nr_tr) +"]/td["+ str(day_nr_td+1) + "]").click()
                    return